from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from .models import ProjectInfo
# Create your views here.


def home(request):
    render(request, 'project/index.html')


class AddProject(View):
    def post(self, request):
        name = request.POST.get('name', None)
        desc = request.POST.get('desc', None)
        browser = request.POST.get('browser', None)
        client = request.POST.get('client', None)
        process_num = request.POST.get('process_num', None)
        if not all([name, desc, browser, client, process_num]):
            return JsonResponse({'status': 401, 'msg': '参数错误！'}, status=401)
        try:
            ProjectInfo.objects.create(name=name, desc=desc, env_browser=browser, env_client=client,
                                   process_num=process_num)
        except Exception as e:
            return JsonResponse({'status': 500, 'msg': f'未知错误--{e}'}, status=500)
        return JsonResponse({'status': 200, 'msg': '项目添加成功！'}, status=200)

    def get(self, request):
        return JsonResponse({'status': 403, 'msg': '访问非法，请重试！'}, status=403)